<?php

namespace Bbs\Controller;

use Common\Controller\BaseController;
use Common\Model\AdModel;
use Common\Model\BbsCountModel;
use Common\Model\BbsModel;
use Common\Model\OrgModel;
use Common\Model\TieModel;
use Common\Model\UserBbsModel;
use Common\Model\UserOrgModel;

class BbsController extends BaseController {

    /**
     * 展示社区BBS
     */
    public function index(){
        $bid = I('get.bid', 0);
        $p = I('get.p', 1);
        $cid = I('get.group', -1);
        $filter = I('get.filter', '');
        $pcount = 20;       // 每页显示数量
        $bbs = BbsModel::getBbsById($bid);
        if(!$bbs){
            $this->error('社区数据异常，请稍后再试！');
        }
        $focused = UID ? UserBbsModel::checkFocused(UID, $bid) : false;
        // 获取团体管理员组
        $admin = UserOrgModel::getOrgUsersInfo($bid, true);
        $where = [
            't.bid' => $bid,
            't.status' => TieModel::NORMAL,
        ];
        if ($cid != -1) {
            $where['t.cid'] = $cid;
        }
        if ($filter == TieModel::ELITE_MARK) {
            $where['t.elite'] = TieModel::ELITE;
        }
        $result = TieModel::getTies($where, $p, $pcount);
        BbsCountModel::update($bid, 'visit');
        $this->assign('page', page2($result['count'], $pcount, $p));
        $this->assign('bbs', $bbs);
        $this->assign('cate', json_decode($bbs['cate'], true));
        $this->assign('focused', $focused);
        $this->assign('admin', $admin);
        $this->assign('index', $p === 1 ? AdModel::getBbsIndexAd() : '');
        $this->assign('list', $result['list']);
        $this->assign('tmp', date('Y-m-d', time()));
        $this->assign('ad', AdModel::getBbsImgAd());
        $this->assign('token', qiniu());
        $this->assign('group', $cid);
        $this->assign('filter', $filter);
        $this->assign('sysNotice', AdModel::getSysNotice());
        $this->assign('title', $bbs['name'].' - 社区'.($p == 1 ? '' : '[第'.$p.'页]'));
        $this->assign('description', $bid['name'].'团体专属社区中的'.($p == 1 ? '所有' : '[第'.$p.'页]').'问答，任何用户都可以进行相互交流、学习和帮助等！'.$bid['desc']);
        $this->display();
    }

    /**
     * 发起话题页面
     */
    public function edit()
    {
        if(!UID){
            $this->error('您需要先登录账号！', U('/login@wooo.wang'));
        }
        $bid = I('get.bid', 0);
        $bbs = BbsModel::getBbsById($bid, false);
        if(!$bbs){
            $this->error('社区数据异常，请刷新后再试！');
        }
        if (IS_POST) {
            $cid = I('post.cid', 0);
            $title = I('post.title', '');
            $html = isset($_POST['html']) ? $_POST['html'] : '';
            if (!$title || !$html) {
                $this->error('参数错误！');
            }
            $data = [
                'bid' => $bid,
                'cid' => $cid,
                'title' => $title,
                'html' => filterXss($html),
                'uid' => UID,
                'create' => time(),
                'update' => time()
            ];
            if(TieModel::createTie($data)){
                $this->success('发起话题成功！', U('/'.$bid));
            }
            $this->error('发起话题失败，请稍后再试！'.json_encode($data));
        } else {
            $this->assign('bbs', $bbs);
            $this->assign('ad', AdModel::getBbsImgAd());
            $this->assign('sysNotice', AdModel::getSysNotice());
            $this->assign('cate', json_decode($bbs['cate'], true));
            $this->assign('token', qiniu());
            $this->display();
        }
    }

    /**
     * 创建新的帖子
     */
    public function publish(){
        if(!UID){
            $this->error('您需要先登录账号！', U('/login@wooo.wang'));
        }
        $oid = I('post.oid', 0);
        $type = I('post.type', 1);
        $title = I('post.title', '');
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        if(!$oid || !$type || !$title || !$html){
            $this->error('数据异常，发布问答失败，请稍后再试！');
        }
        $data = array(
            'oid' => $oid,
            'title' => $title,
            'html' => filterXss($html),
            'uid' => UID,
            'create' => time(),
            'update' => time()
        );
        $tid = TieModel::createTie($oid, $data);
        if(false === $tid){
            $this->error('系统异常，发布问答失败，请稍后再试！');
        }
        $this->success('发布问答成功！', U('/bbs/'.$oid));
    }

    /**
     * 发布帖子的补充说明
     */
    public function publishpatch(){
        if(!UID){
            $this->error('您需要先登录账号！', U('/login@wooo.wang'));
        }
        $tid = I('post.tid', 0);
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        if(!$tid || !$html){
            $this->error('数据异常，请稍后再试！');
        }
        if(!TieModel::fix($tid, $html)){
            $this->error('系统异常，请稍后再试！');
        }
        $this->success('补充成功！', U('/t/'.$tid));
    }




    /**
     * 展示BBS中的所有精华问答
     */
    public function elite(){
        $oid = I('get.oid', 0);
        $p = I('get.p', 1);
        // $type = I('get.t', 1);  // type用于后期的分级，现暂未使用
        $pcount = 10;       // 每页显示数量
        $org = null;
        if(!$oid || !$org = OrgModel::getOrgById($oid)){
            $this->error('数据异常，请稍后再试！');
        }
        $focused = UID ? UserBbsModel::checkFocused(UID, $oid) : false;
        // 获取团体管理员组
        $admin = UserOrgModel::getOrgUsersInfo($oid, true);
        $result = TieModel::getEliteList($oid, $p, $pcount);
        $this->assign('page', page($result['count'], $pcount, $p));
        $this->assign('org', $org);
        $this->assign('focused', $focused);
        $this->assign('admin', $admin);
        $this->assign('list', $result['list']);
        $this->assign('tmp', date('Y-m-d', time()));
        $this->assign('ad', AdModel::getBbsImgAd());
        $this->assign('index', $p === 1 ? AdModel::getBbsIndexAd() : '');
        $this->assign('token', qiniu());
        $this->assign('title', $org['name'].' - 精 - 社区'.($p == 1 ? '' : '[第'.$p.'页]'));
        $this->assign('description', $org['name'].'团体专属社区中的'.($p == 1 ? '所有' : '[第'.$p.'页]').'精华问答，是社区管理员筛选设置最有价值的问答，方便用户快捷阅览！'.$org['desc']);
        $this->display();
    }

    /**
     * 展示社区BBS中的所有文档
     */
    public function docs(){
        $oid = I('get.oid', 0);
        $org = null;
        if(!$oid || !$org = OrgModel::getOrgById($oid)){
            $this->error('数据异常，请稍后再试！');
        }
        $focused = UID ? UserBbsModel::checkFocused(UID, $oid) : false;
        $admin = UserOrgModel::getOrgUsersInfo($oid, true);
        $docs = OrgDocModel::getOrgDocsInfo($oid);
        $this->assign('org', $org);
        $this->assign('list', $docs);
        $this->assign('focused', $focused);
        $this->assign('admin', $admin);
        $this->assign('tmp', date('Y-m-d', time()));
        $this->assign('ad', AdModel::getBbsImgAd());
        $this->assign('token', qiniu());
        $this->assign('title', $org['name'].' - 文档 - 社区');
        $this->assign('description', $org['name'].'团体专属社区中的所有文档，是由所有用户共同编辑修改，由'.$org['name'].'社区管理员审阅而进行迭代更新的文档！'.$org['desc']);
        $this->display();
    }




    /**
     * 用户捐赠社区/团体
     */
    public function donation(){
        if(!UID){
            json(204, '登陆后再进行操作');
        }
        $bid = I('get.bid', 0);
        $gx = I('post.gx', 0);
        if(!$bid || !$gx){
            json(204, '数据异常，请稍后再试');
        }
        if(UserBbsModel::donation(UID, $bid, $gx)){
            json(200, '十分感谢您的捐赠，愿你每天好心情');
        }
        json(204, '捐赠失败，请稍后再试');
    }

    /**
     * Ajax 关注与取消关注BBS
     * 接收到的type为1表示关注，为2表示取消关注
     */
    public function ajaxfocus(){
        $bid = I('post.bid', 0);
        $type = I('post.type', 0);      // 为1表示关注，为2表示取消关注
        if(!UID){
            json(400, '您需要登陆之后才能关注BBS！');
        }
        if(!$bid || !$type){
            json(400, '系统数据异常，请您稍候再试！');
        }
        if($type == 1){     // 表示关注BBS
            if(UserBbsModel::checkFocused(UID, $bid)){
                json(400, '您已经关注过该社区，请勿重复关注！');
            }
            if ($this->user['gx'] < 1) {
                json(400, '关注社区需要 1 点贡献度，您的贡献度不足！');
            }
            $result = UserBbsModel::focus(UID, $bid);
            if(false !== $result){
                json(200);
            }
            json(400, '系统异常，关注BBS失败，请您稍后再试！');
        }else if($type == 2){
            if(!UserBbsModel::checkFocused(UID, $bid)){
                json(400, '您还未关注该BBS，请先关注！');
            }
            $result = UserBbsModel::cancelFocus(UID, $bid);
            if(false !== $result){
                json(200);
            }
            json(400, '系统异常，取消关注失败，请您稍后再试！');
        }else{
            json(400,'收藏系统出现异常,请您稍候再试！');
        }
    }

}
